package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.repository.DestinationMongoRepository;
import cn.wolfcode.wolf2w.repository.StrategyCatalogMongoRepository;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    
    @Autowired
    private StrategyCatalogMongoRepository strategyCatalogMongoRepository;

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        strategyCatalogMongoRepository.save(strategyCatalog);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        strategyCatalogMongoRepository.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
        strategyCatalogMongoRepository.deleteById(id);
    }

    @Override
    public StrategyCatalog get(String id) {
        return strategyCatalogMongoRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyCatalog> list() {
        return strategyCatalogMongoRepository.findAll();
    }

    @Override
    public Page<StrategyCatalog> query(QueryObject qo) {
        // 创建查询对象
        Query query = new Query();

        // 查询总条数
        long count = template.count(query, StrategyCatalog.class);
        // 调用PageRequest 方法进行分页
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        // 给查询对象设置条件
        query.with(pageable);
        // 从mongodb 中查询符合条件的数据
        List<StrategyCatalog> strategyCatalogs = template.find(query, StrategyCatalog.class);
        // 返回当前页数据,分页和总长度
        return new PageImpl<>(strategyCatalogs , pageable , count);
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        if (StringUtils.hasLength(strategyCatalog.getId())){
            this.update(strategyCatalog);
        }else {
            this.save(strategyCatalog);
        }
    }

    @Override
    public List<CatalogVO> queryGroupList() {
        List<CatalogVO> list = new ArrayList<>();
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
                Aggregation.group("destName").
                        push("name").as("names").
                        push("id").as("ids")
        );
        AggregationResults<Map> result = template.aggregate(agg,Map.class);
        List<Map> datas = result.getMappedResults();
        for (Map data : datas) {
            CatalogVO vo = new CatalogVO();
            vo.setDestName(data.get("_id").toString());
            List<Map<String, Object>> mm = new ArrayList<>();
            List<Object> names = (List<Object>) data.get("names");
            List<Object> ids = (List<Object>) data.get("ids");
            for (int i = 0;i < names.size(); i++) {
                Map<String, Object> vv = new HashMap<>();
                String name = names.get(i).toString();
                String id = ids.get(i).toString();
                vv.put("id", id);
                vv.put("name", name);
                mm.add(vv);
            }
            vo.setMapList(mm);
            list.add(vo);
        }
        System.out.println(list);
        return list;
    }

    @Override
    public List<StrategyCatalog> queryCatalogByDestId(String id) {
        // 通过destId 查找所有攻略分类
        List<StrategyCatalog> list = strategyCatalogMongoRepository.findByDestId(id);

        // 遍历攻略分类
        for (StrategyCatalog strategyCatalog:list) {
            // 通过攻略id 查询攻略  strategyCatalog.getId() = strategie.catalogId
            List<Strategy> strategies = strategyService.queryCatalogId(strategyCatalog.getId());
            // 把查询到的攻略放进对应的攻略分类中
            strategyCatalog.setStrategies(strategies);
        }
        return list;
    }

}
